package com.dsz.duty.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import web.models.CommonResult;
import web.models.Page;

import com.dsz.duty.dao.IDutyRecordDao;
import com.dsz.duty.model.DutyRecord;
import com.dsz.duty.model.User;

/**
 * 值班记录 MVC Controller。
 * 
 * @author crjun
 */
@Controller
@RequestMapping("dutyrecord")
public class DutyRecordController {
	@Resource
	private IDutyRecordDao dutyRecordDao;
	
	public void setDutyRecordDao(IDutyRecordDao dutyRecordDao) {
		this.dutyRecordDao = dutyRecordDao;
	}

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult addDutyRecord(@ModelAttribute DutyRecord dutyRecord, HttpServletRequest request) {
    	dutyRecord.setDutyMan(((User)(request.getSession().getAttribute("currentUser"))).getUsername());
    	dutyRecord.setDutyManFullName(((User)(request.getSession().getAttribute("currentUser"))).getFullName());
    	dutyRecordDao.addDutyRecord(dutyRecord);
        return new CommonResult(new Integer(1));
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateDutyRecord(@ModelAttribute DutyRecord dutyRecord) {
    	dutyRecordDao.updateDutyRecord(dutyRecord);
        return new CommonResult(new Integer(1));
    }

    @RequestMapping(value = "/remove/{dutyRecordId}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult removeDutyRecord(@PathVariable int dutyRecordId) {
    	dutyRecordDao.removeDutyRecord(dutyRecordId);
        return new CommonResult(new Integer(1));
    }

    @RequestMapping("/query")
    @ResponseBody
    public Page<DutyRecord> queryDutyRecords(@RequestParam String dutyMan, @RequestParam String dutyTime, @RequestParam int page, @RequestParam int rows) {
//    	public Page<DutyRecord> queryDutyRecords(@RequestParam String dutyMan, @RequestParam String dutyTime, @RequestParam String entryTime, @RequestParam int page, @RequestParam int rows) {
    	String filter = "";
    	boolean flag = false;
    	if (null != dutyMan && !dutyMan.trim().equals("")) {
    		filter += "dutyManFullName like '%" + dutyMan + "%'";
    		flag = true;
    	}
    	if (null != dutyTime && !dutyTime.trim().equals("")) {
    		if (flag) {
    			filter += " and ";
    		}
    		filter += "dutyTime = '" + dutyTime + "'";
    	}
//    	if (null != entryTime && !entryTime.trim().equals("")) {
//    		if (!filter.equals("")) {
//    			filter += " and ";
//    		}
//    		filter += "entryTime = '" + entryTime + "'";
//    	}
        List<DutyRecord> dutyRecords = dutyRecordDao.getPageDutyRecords(filter, page, rows);
        int count = dutyRecordDao.getDutyRecordsCount(filter);
        return new Page<DutyRecord>(count, dutyRecords);
    }

    @RequestMapping("/queryone/{dutyRecordId}")
    @ResponseBody
    public DutyRecord queryDutyRecord(@PathVariable String dutyRecordId) {
    	String filter = "id=" + dutyRecordId;
        List<DutyRecord> dutyRecords = dutyRecordDao.getPageDutyRecords(filter, 1, 1);
        if (null != dutyRecords)
        	return dutyRecords.get(0);
        return null;
    }
}
